package com.ny.service.impl;

import com.ny.mapper.FinancialTransactionMapper;
import com.ny.mapper.MemberCardMapper;
import com.ny.mapper.MemberConsumptionMapper;
import com.ny.mapper.ProductPurchaseMapper;
import com.ny.domain.FinancialTransactionDO;
import com.ny.domain.MemberCardDO;
import com.ny.domain.MemberConsumptionDO;
import com.ny.domain.ProductPurchaseDO;
import com.ny.query.MemberIdQuery;
import com.ny.service.FinanceService;
import java.lang.Override;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ny.exception.BusinessException;
import org.springframework.util.CollectionUtils;

/**
 * 财务管理模块的实现
 *
 * @author MSI-NB
 * @date 2025-03-08 13:14:38
 */
@Service
public class FinanceServiceImpl implements FinanceService {

    @Autowired
    private MemberConsumptionMapper memberConsumptionMapper;

    @Autowired
    private MemberCardMapper memberCardMapper;

    @Autowired
    private ProductPurchaseMapper productPurchaseMapper;

    @Autowired
    private FinancialTransactionMapper financialTransactionMapper;

    @Override
    public List<MemberConsumptionDO> queryMemberConsumption(MemberIdQuery query) {
        // 检查会员ID是否存在
        if (memberConsumptionMapper.selectByMemberId(query.getMemberId()).isEmpty()) {
            throw new BusinessException("会员ID不存在");
        }
        // 通过会员ID查询消费记录
        List<MemberConsumptionDO> consumptionRecords = memberConsumptionMapper.selectByMemberId(query.getMemberId());
        return consumptionRecords;
    }

    @Override
    public List<FinancialTransactionDO> queryFinancialTransactions() {
        // 查询所有资金交易记录
        List<FinancialTransactionDO> transactions = financialTransactionMapper.selectAll();
        return transactions;
    }

    @Override
    public List<MemberCardDO> queryMemberCard(MemberIdQuery query) {
        // 检查会员ID是否存在
        if (CollectionUtils.isEmpty(memberCardMapper.selectAllByMemberId(query.getMemberId()))) {
            throw new BusinessException("会员ID不存在");
        }
        // 通过会员ID查询会员卡信息
        List<MemberCardDO> memberCards = memberCardMapper.selectAllByMemberId(query.getMemberId());
        return memberCards;
    }

    @Override
    public List<MemberCardDO> queryMemberCardRecharge() {
        // 查询所有会员卡充值记录
        List<MemberCardDO> rechargeRecords = memberCardMapper.selectAllRechargeRecords();
        return rechargeRecords;
    }

    @Override
    public List<ProductPurchaseDO> queryProductPurchases() {
        // 查询所有商品销售记录
        List<ProductPurchaseDO> purchases = productPurchaseMapper.selectAll();
        return purchases;
    }

    @Override
    public List<ProductPurchaseDO> queryMemberProductPurchases(MemberIdQuery query) {
        // 通过会员ID查询商品购买记录
        List<ProductPurchaseDO> memberPurchases = productPurchaseMapper.selectByMemberId(query.getMemberId());
        return memberPurchases;
    }
}
